package com.combetter.changxing.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;


/**
 * Created by luoren on 15/12/11.
 */
public class KeyBoardUtil {


    private KeyBoardUtil() {
    }

    /**
     * 强制显示输入法
     */
    public static void show(Activity activity) {
        show(activity.getWindow().getCurrentFocus());
    }

    public static void show(View view) {
       if (view!=null)
        getInputMethodManager(view.getContext()).showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 强制关闭输入法
     */
    public static void hide(Activity activity) {
        hide(activity.getWindow().getCurrentFocus());
    }

    public static void hide(View view) {
        if (view != null) {
            getInputMethodManager(view.getContext()).hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 如果输入法已经显示，那么就隐藏它；如果输入法现在没显示，那么就显示它
     */
    public static void showOrHide(View view) {
        if (view != null) {
            getInputMethodManager(view.getContext()).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public static InputMethodManager getInputMethodManager(Context context){
        return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

}
